# Top level makefile fragment for JAL For GCC. -*- makefile -*-

#   Copyright (C) 2010 Albert Faber

# GCC is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.

# GCC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with GCC; see the file COPYING3.  If not see
# <http://www.gnu.org/licenses/>.


# This file provides the language dependent support in the main Makefile.
# Each language makefile fragment must provide the following targets:
#
# foo.all.cross, foo.start.encap, x	foo.rest.encap,
# foo.install-common, foo.install-man, foo.install-info, foo.install-pdf,
# foo.info, foo.dvi, foo.pdf, foo.html, foo.uninstall,
# foo.mostlyclean, foo.clean, foo.distclean,
# foo.maintainer-clean, foo.stage1, foo.stage2, foo.stage3, foo.stage4
#
# where `foo' is the name of the language.
#
# It should also provide rules for:
#
# - making any compiler driver (eg: gcc)
# - the compiler proper (eg: tree1)
# - define the names for selecting the language in LANGUAGES.


## note program-prefix and program-suffix options are not supported
## just program_transform_name which is a sed script to transform the
## names

JALSED = sed
JALSEDFLAGS = -n

GCC_EXTRAS = -B./ -B$(build_tooldir)/bin/ -isystem $(build_tooldir)/include

# ./xgcc is the just built compiler. See GCC_FOR_TARGET in the GCC Makefile.in.
# If this can't be found, you probably have not done a bootstrap of GCC,
# which you need to do.

# GCC_FOR_JAL = ./xgcc $(GCC_EXTRAS)


JAL_TARGET_OBJS=jal/jal-pretty-print.o jal/jal-gimplify.o jal/jal-aux-info.o jal/jal-convert.o jal/jal-dump.o jal/jal-common.o jal/jal-decl.o jal/jal-typeck.o jal/jal-pragma.o jal/jal-parser.o jal/jal-lex.o jal/jal-cpp.o jal/jal-lang.o jal/jal-opts.o

# Language-specific object files for C and Objective C.
JAL_AND_OBJC_OBJS = attribs.o  \
  $(C_TARGET_OBJS) c-common.o c-format.o c-semantics.o \
  incpath.o cppdefault.o c-ppoutput.o c-cppbuiltin.o prefix.o \
  c-pch.o $(JAL_TARGET_OBJS) \
  tree-mudflap.o c-omp.o

# Language-specific object files for C.
JAL_OBJS = stub-objc.o $(JAL_AND_OBJC_OBJS)


TREE_GENERATED = lex.c parse.c parse.h parse.output
# We need to use something other than JAL here because the directory
# is called JAL
TREE_EXES = gjal

#strict warnings for JAL
jal-warn = $(STRICT_WARN)
# bison emits some unused static arrays (yytoknum)
jal/parse.o-warn = -Wno-error
# flex produces 'no previous prototype' warnings
jal/lex.o-warn = -Wno-error

#
# Define the names for selecting jal in LANGUAGES.

.phony: jal

jal:jal.done

jal.done: gjal$(exeext) jal1$(exeext)
	$(STAMP) jal.done

# no preprocessor

# core compiler
jal1$(exeext): \
	$(BACKEND) $(LIBSDEPS) attribs.o $(JAL_TARGET_OBJS)
	$(CC) $(ALL_CFLAGS) $(LDFLAGS) -o $@ \
	$(JAL_OBJS) cc1-checksum.o \
	$(BACKEND) $(LIBS) $(GMPLIBS)

# Create the compiler driver jal.
gjal$(exeext): $(GCC_OBJS) version.o prefix.o intl.o $(EXTRA_GCC_OBJS) \
  $(LIBDEPS) jal/spec.o
	$(CC) $(ALL_CFLAGS) $(LDFLAGS) -o $@ jal/spec.o \
	  $(GCC_OBJS) version.o prefix.o intl.o $(EXTRA_GCC_OBJS) $(LIBS)


# Create a version of the JAL driver which calls the cross-compiler.
jal-cross$(exeext): jal1$(exeext)
	-rm -f jal-cross$(exeext)
	cp jal1$(exeext) jal-cross$(exeext)


# Compiling object files from source files.

# object file makes

jal/jal-pretty-print.o: jal/jal-pretty-print.c $(CONFIG_H) $(SYSTEM_H) jal/jal-pretty-print.h jal/jal-common.h jal/jal.h \
  coretypes.h $(DIAGNOSTIC_H) $(TREE_H) $(FLAGS_H) toplev.h langhooks.h $(TM_H)

jal/jal-gimplify.o: jal/jal-gimplify.c $(CONFIG_H) $(SYSTEM_H) jal/jal-common.h jal/jal.h \
  coretypes.h $(DIAGNOSTIC_H) $(TREE_H) $(FLAGS_H) toplev.h langhooks.h $(TM_H)

jal/jal-aux-info.o: jal/jal-aux-info.c $(CONFIG_H) $(SYSTEM_H) jal/jal-common.h jal/jal.h \
  coretypes.h $(DIAGNOSTIC_H) $(TREE_H) $(FLAGS_H) toplev.h langhooks.h $(TM_H)

jal/jal-convert.o: jal/jal-convert.c $(CONFIG_H) $(SYSTEM_H) jal/jal-common.h jal/jal.h \
  coretypes.h $(DIAGNOSTIC_H) $(TREE_H) $(FLAGS_H) toplev.h langhooks.h $(TM_H)

jal/jal-common.o: jal/jal-common.c $(CONFIG_H) $(SYSTEM_H) jal/jal-common.h jal/jal.h \
  coretypes.h $(DIAGNOSTIC_H) $(TREE_H) $(FLAGS_H) toplev.h langhooks.h $(TM_H)

jal/jal-dump.o: jal/jal-dump.c $(CONFIG_H) $(SYSTEM_H) jal/jal-common.h jal/jal.h \
  coretypes.h $(DIAGNOSTIC_H) $(TREE_H) $(FLAGS_H) toplev.h langhooks.h $(TM_H)


jal/spec.o: jal/spec.c $(CONFIG_H) $(SYSTEM_H) \
  coretypes.h $(DIAGNOSTIC_H) $(TREE_H) $(FLAGS_H) toplev.h langhooks.h $(TM_H)

jal/jal-cpp.o: jal/jal-cpp.c $(CONFIG_H) $(SYSTEM_H) jal/jal-cpp.h \
  coretypes.h $(DIAGNOSTIC_H) $(TREE_H) $(FLAGS_H) toplev.h langhooks.h $(TM_H)

jal/jal-lex.o: jal/jal-lex.c $(CONFIG_H) $(SYSTEM_H) jal/jal-lex.h \
  coretypes.h $(DIAGNOSTIC_H) $(TREE_H) $(FLAGS_H) toplev.h langhooks.h $(TM_H)

jal/jal-typeck.o: jal/jal-typeck.c $(CONFIG_H) $(SYSTEM_H) jal/jal-lex.h \
  coretypes.h $(DIAGNOSTIC_H) $(TREE_H) $(FLAGS_H) toplev.h langhooks.h $(TM_H)

jal/jal-decl.o: jal/jal-decl.c $(CONFIG_H) $(SYSTEM_H) jal/jal-lex.h \
  coretypes.h $(DIAGNOSTIC_H) $(TREE_H) $(FLAGS_H) toplev.h langhooks.h $(TM_H)

jal/jal-opts.o: jal/jal-opts.c jal/jal-opts.h $(CONFIG_H) $(SYSTEM_H) \
  coretypes.h $(DIAGNOSTIC_H) $(TREE_H) $(FLAGS_H) toplev.h langhooks.h $(TM_H)

jal/jal-pragma.o: jal/jal-pragma.c jal/jal-pragma.h $(CONFIG_H) $(SYSTEM_H) \
  coretypes.h $(DIAGNOSTIC_H) $(TREE_H) $(FLAGS_H) toplev.h langhooks.h $(TM_H)


jal/jal-parser.o: jal/jal-parser.c $(CONFIG_H) $(SYSTEM_H) \
  coretypes.h $(DIAGNOSTIC_H) $(TREE_H) $(FLAGS_H) toplev.h langhooks.h $(TM_H)
  
jal/jal-lang.o: jal/jal-lang.c jal/jal-opts.h $(CONFIG_H) $(SYSTEM_H) \
  coretypes.h $(DIAGNOSTIC_H) $(TREE_H) $(FLAGS_H) toplev.h langhooks.h $(TM_H)

# generated files the files from lex and yacc are put into the source
# directory in case someone wants to build but does not have
# lex/yacc

# Build hooks:

jal.all.cross: jal-cross$(exeext)

jal.start.encap:
jal.rest.encap:
jal.man:
jal.srcman:

jal.tags: force
	cd $(srcdir)/jal; etags -o TAGS.sub *.y *.l *.c *.h; \
	etags --include TAGS.sub --include ../TAGS.sub

jal.info: doc/jal.info

jal.srcinfo: doc/jal.info
	-cp -p $^ $(srcdir)/doc

jal.dvi: doc/jal.dvi

JAL_PDFFILES = doc/jal.pdf

jal.pdf: $(JAL_PDFFILES)

TEXI_JAL_FILES = jal/jal.texi \
	 $(gcc_docdir)/include/gcc-common.texi \
	 $(gcc_docdir)/include/gpl.texi \
	 $(gcc_docdir)/include/fdl.texi \
	 $(gcc_docdir)/include/funding.texi \
	 gcc-vers.texi

doc/jal.info: $(TEXI_JAL_FILES)
	$(MAKEINFO) $(MAKEINFOFLAGS) -I $(gcc_docdir)/include -o $@ $<

doc/jal.dvi: $(TEXI_JAL_FILES)
	$(TEXI2DVI) -I $(abs_docdir)/include -o $@ $<

doc/jal.pdf: $(TEXI_JAL_FILES)
	$(TEXI2PDF) -I $(abs_docdir)/include -o $@ $<

jal.html: $(build_htmldir)/jal/index.html

$(build_htmldir)/jal/index.html: $(TEXI_JAL_FILES)
	$(mkinstalldirs) $(@D)
	rm -f $(@D)/*
	$(TEXI2HTML) -I $(gcc_docdir)/include -I $(srcdir)/jal -o $(@D) $<

#
# Install hooks:

# Install
.phony:jal.install.common
.phony:jal.install

jal.install jal.install.common jal.install-common: jal.install.common.done

jal.install.common.done:  installdirs jal.done
	for name in $(TREE_EXES); \
	do \
	   if [ -f $$name ] ; then \
	    name2="`echo \`basename $$name\` | sed -e '$(program_transform_name)' `"; \
	    rm -f $(DESTDIR)$(bindir)/$$name2$(exeext); \
	    echo $(INSTALL_PROGRAM) $$name$(exeext) $(DESTDIR)$(bindir)/$$name2$(exeext); \
	    $(INSTALL_PROGRAM) $$name$(exeext) $(DESTDIR)$(bindir)/$$name2$(exeext); \
	    chmod a+x $(DESTDIR)$(bindir)/$$name2$(exeext); \
	  fi ; \
	done
	$(STAMP) jal.install.common.done

# We might not be able to build the info files
jal.install-info: $(DESTDIR)$(infodir)/jal.info

jal.install-pdf: $(JAL_PDFFILES)
	@$(NORMAL_INSTALL)
	test -z "$(pdfdir)/gcc" || $(mkinstalldirs) "$(DESTDIR)$(pdfdir)/gcc"
	@list='$(JAL_PDFFILES)'; for p in $$list; do \
	  if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \
	  f=$(pdf__strip_dir) \
	  echo " $(INSTALL_DATA) '$$d$$p' '$(DESTDIR)$(pdfdir)/gcc/$$f'"; \
	  $(INSTALL_DATA) "$$d$$p" "$(DESTDIR)$(pdfdir)/gcc/$$f"; \
	done

jal.install-man:

jal.uninstall:
	for name in $(TREE_EXES); \
	do \
	  echo $$name; \
	  name2="`echo $$name | sed -e '$(program_transform_name)' `"; \
	  echo becomes $$name2; \
	  echo -rm -rf $(DESTDIR)$(bindir)/$$name2$(exeext); \
	  rm -rf $(DESTDIR)$(bindir)/$$name2$(exeext); \
	done
	-rm jal.install.common.done

#
# Clean hooks:
# A lot of the ancillary files are deleted by the main makefile.
# We just have to delete files specific to us.

jal.mostlyclean:
	for name in $(TREE_EXES); \
	do \
	  echo deleting $$name; \
	  if [ -f jal/$$name$(exeext) ] ; then \
	    rm -f jal/$$name$(exeext); \
	  fi ; \
	done
	-rm -f jal/*$(objext)
	-rm -f jal/*$(coverageexts)
	-rm jal.done


jal.clean: jal.mostlyclean

jal.distclean: jal.clean
	-rm -f jal/config.status
	-rm -f jal/*.output

jal.maintainer-clean: jal.distclean
	for name in $(TREE_GENERATED); \
	do \
	  if [ -f $(srcdir)/jal/$$name ] ; then \
             echo deleting $(srcdir)/jal/$$name; \
	     rm -f $(srcdir)/jal/$$name; \
	  fi ; \
	done
	-rm -R $(srcdir)/jal/*~


#
# Stage hooks:
# The main makefile has already created stage?/jal.

jal.stage1: stage1-start
	-mv jal/*$(objext) stage1/jal
jal.stage2: stage2-start
	-mv jal/*$(objext) stage2/jal
jal.stage3: stage3-start
	-mv jal/*$(objext) stage3/jal
jal.stage4: stage4-start
	-mv jal/*$(objext) stage4/jal
jal.stageprofile: stageprofile-start
	-mv jal/*$(objext) stageprofile/jal
jal.stagefeedback: stagefeedback-start
	-mv jal/*$(objext) stagefeedback/jal

# Maintenance hooks:

# test hook

check-jal: jal.check

TESTSUITEDIR = testsuite

jal.check: $(TESTSUITEDIR)/site.exp
	-mkdir testsuite/jal

# these three files are empty and it seems diff has trouble generating
# patch files for new empty files as required for cvs.
# STAMP does not cut it here as I need an empty file.
	-rootme=`${PWD_COMMAND}`; export rootme; \
	srcdir=`cd ${srcdir}; ${PWD_COMMAND}` ; export srcdir ; \
	cd testsuite; \
	EXPECT=${EXPECT} ; export EXPECT ; \
	if [ -f $${rootme}/../expect/expect ] ; then  \
	   TCL_LIBRARY=`cd .. ; cd ${srcdir}/../tcl/library ; ${PWD_COMMAND}` ; \
	   export TCL_LIBRARY ; fi ; \
	PATH=`cd ..;${PWD_COMMAND}`:$$PATH; export PATH; \
	gcc_extras="-B`cd ..;${PWD_COMMAND}` -B`cd ..;${PWD_COMMAND}`/jal"; export gcc_extras; \
	$(RUNTEST) --tool jal $(RUNTESTFLAGS)

# copy the output files from the current test to source ie say the new results are OK
jal.check.fix: force
	srcdir=`cd ${srcdir}; ${PWD_COMMAND}` ; export srcdir ;
	-cp testsuite/jal/*.out* t
	-cp testsuite/jal/*runpgm* t
	-rm -f t/*nofilename

config.status: jal/config-lang.in
